package tech.aistar.prj.controller;

import tech.aistar.prj.entity.User;
import tech.aistar.prj.service.IUserService;
import tech.aistar.prj.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;

/**
 * 本类功能:
 *
 * @author cxylk
 * @date 2020/9/18 18:14
 */
@WebServlet(urlPatterns = "/user/*")
public class UserHandlerAction extends HttpServlet {
    private IUserService service=new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取模糊匹配的路径
        String pathInfo=req.getPathInfo();

        switch (pathInfo){
            case "/exit":
                //安全退出
                //清空session
                req.getSession().invalidate();

                //重定向到登录的页面
                resp.sendRedirect("/lk/user/loginView");
                break;
            case "/logincl":
                //1.处理resp请求乱码的问题(req的乱码问题在server.xml文件中已处理)
                resp.setCharacterEncoding("utf-8");
                //2.接收表单数据
                String username=req.getParameter("username");
                String password=req.getParameter("password");

                User user=service.login(username);

                //进行各种判断
                if(null!=user){
                    if(user.getPassword().equals(password)){
                        //将user对象放入到session作用域
                        req.getSession().setAttribute("user",user);

                        //判断是否勾选了记住密码
                        String flag=req.getParameter("flag");

                        if(null!=flag) {
                            //对username进行一级编码
                            username = URLEncoder.encode(username, "utf-8");
                            //继续对username进行二级编码
                            username = URLEncoder.encode(username, "utf-8");

                            //将登陆成功之后的username以及password放入到cookie对象中
                            Cookie c = new Cookie("loginInfo", username + ":" + password);
                            //还可以设置一些可选的属性
                            c.setPath("/");//设置域(代表当前根目录下)

                            //设置过期时间 - 单位是s
                            c.setMaxAge(60);

                            //2.通过响应头将这个cookie发送到客户端
                            resp.addCookie(c);
                        }else{
                            Cookie c=new Cookie("loginInfo","");
                            c.setPath("/");
                            resp.addCookie(c);
                        }
                        //登录成功...
                        resp.sendRedirect("/lk/pet/indexData");
                    }
                    else{
                        //密码错误
                        //设置个error参数判断是否存在用户
//                resp.sendRedirect("/lk/user/loginView?error=1");

                        req.setAttribute("error","1");
                        req.setAttribute("username",username);
                        req.setAttribute("password",password);

                        //为了进行数据的回显功能
                        req.getRequestDispatcher("/user/loginView").forward(req,resp);

                    }
                }else{
                    //用户是不存在的，不存在跳转到view界面
//            resp.sendRedirect("/lk/user/loginView?error=0");

                    req.setAttribute("error","0");
                    req.setAttribute("username",username);
                    req.setAttribute("password",password);

                    req.getRequestDispatcher("/user/loginView").forward(req,resp);
                }
                break;
        }
    }
}
